package com.itianshan.wheel;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import com.bluetooth.BluetoothIOClass;
import com.bluetooth.ble.BluetoothLeService;
import com.tencent.liteav.chat.ChatRoomClass;

import java.util.ArrayList;
import java.util.List;

public class CloudBluetoothCarActivity extends CloudBaseActivity {
    private int                             mGrantedCount = 0;                           // 权限个数计数，获取Android系统权限
    private static final String             deviceType = "cloud_bluetooth_car";          //设备类型，bluetooth 和 usb
    public static final String              EXTRAS_DEVICE_NAME = "DEVICE_NAME";          //蓝牙设备名称
    public static final String              EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";    //蓝牙地址
    private BluetoothIOClass                bluetoothIO;                                //蓝牙操作器
    private String                          mDeviceName;                                //蓝牙名称
    private String                          mDeviceAddress;                             //蓝牙地址
    private TextView                        mConnectionState;                           //展示蓝牙连接状态
    private TextView                        roomState;                                  //展示房间连接状态
    private long                            lastCommandTime=0;                          //最后接收到控制信号的时间，超时停止小车行动。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 先检查权限再加入通话
        if (checkPermission()) {
            initView(); //配置外观,该配置在父类
            enterBluetoothRoom(); //蓝牙操作
        }
    }
    /*
     * 蓝牙控制小车
     */
    private void enterBluetoothRoom(){
        final Intent intent = getIntent();
        mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
        mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
        mConnectionState = findViewById(R.id.connection_state);
        roomState = findViewById(R.id.connection_room_state);
        roomState.setText("");

        bluetoothIO = new BluetoothIOClass(mDeviceAddress,mDeviceName,true);
        bluetoothIO.mConnectionState=mConnectionState;
        registerReceiver(bluetoothIO.mGattUpdateReceiver, bluetoothIO.makeGattUpdateIntentFilter());
        Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
        bindService(gattServiceIntent, bluetoothIO.mServiceConnection, BIND_AUTO_CREATE);

        chatRoomClass = new ChatRoomClass(mRoomId,mUserId,mIsFrontCamera);
        chatRoomClass.setLocalPreviewView(mLocalPreviewView); //传入远端用户显示用到的控件
        chatRoomClass.setRemoteUserViews(mRemoteViewList); //传入远端用户显示用到的控件
        chatRoomClass.setCloudType(deviceType);
        chatRoomClass.setBluetoothIO(bluetoothIO);

        chatRoomClass.prepareToEnterRoom(getApplicationContext(),roomState);  //准备进入房间，服务器获取签名

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //断开蓝牙连接
        unbindService(bluetoothIO.mServiceConnection);
        if (bluetoothIO.mBluetoothLeService != null) {
            bluetoothIO.mBluetoothLeService.disconnect();
        }
        unregisterReceiver(bluetoothIO.mGattUpdateReceiver);
        bluetoothIO.mBluetoothLeService = null;
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//恢复竖屏
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQ_PERMISSION_CODE) {
            for (int ret : grantResults) {
                if (PackageManager.PERMISSION_GRANTED == ret) mGrantedCount++;
            }
            if (mGrantedCount == permissions.length) {
                //首次启动，权限都获取到，才能正常进入通话。
                enterBluetoothRoom();
            } else {
                Toast.makeText(this, getString(R.string.rtc_permisson_error_tip), Toast.LENGTH_SHORT).show();
            }
            mGrantedCount = 0;
        }
    }
}
